package com.example.hasee.showinfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.hasee.data.entity.School;
import com.example.hasee.libraryreservation.R;

import java.util.List;

public class SchoolAdapter extends BaseAdapter {
    private Context mContext;
    private List<School> mData;

    public SchoolAdapter(Context context,List<School> data) {
        mContext = context;
        mData=data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    //获取没一行Item的显示内容。
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        //如果view未被实例化过，缓存池中没有对应的缓存
        if(convertView==null){
            //将XML转化为View
            convertView=LayoutInflater.from(mContext).inflate(R.layout.item_school,parent,false);
            //对viewHolder的属性进行赋值
            holder=new ViewHolder();
            //从条目中获取控件，保存到缓存对象中
            holder.tvName=convertView.findViewById(R.id.tvName);
            holder.tvNo=convertView.findViewById(R.id.tvNo);
            holder.tvGroup=convertView.findViewById(R.id.tvGroup);
            holder.tvSubject=convertView.findViewById(R.id.tvSubject);
            holder.tvYear=convertView.findViewById(R.id.tvYear);
            //通过setTag将convertView与viewHolder关联
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }
        // 取出School对象
        School school=mData.get(position);
        // 设置控件的数据
        holder.tvName.setText(school.getName());
        holder.tvNo.setText("专业代码:"+school.getNo());
        holder.tvSubject.setText("专业名称:"+school.getSubject());
        holder.tvGroup.setText("所属学科:"+school.getGroup());
        holder.tvYear.setText("修业年限:"+school.getYear());
        return convertView;
    }
    private class ViewHolder{
        TextView tvName;
        TextView tvNo;
        TextView tvSubject;
        TextView tvYear;
        TextView tvGroup;
    }
    /*总结一下用ViewHolder优化BaseAdapter的整体步骤：
    1 创建bean对象，用于封装数据；
    2 在构造方法中初始化的数据List；
    3 创建ViewHolder类，创建布局映射关系；
    4 判断convertView，为空则创建，并设置tag，不为空则通过tag取出ViewHolder；
    5 给ViewHolder的控件设置数据。*/
}
